package vn.com.vega.chacha.v2;

import java.util.List;

import vn.com.vega.chacha.R;
import vn.com.vega.music.downloadmanager.DownloadUtil;
import vn.com.vega.music.objects.Song;
import vn.com.vega.music.utils.AnimationChaCha;
import vn.com.vega.music.utils.PlaylistUtil;
import vn.com.vega.music.utils.Util;
import vn.com.vega.music.view.adapter.SelectSongPlayAdapter;
import vn.com.vega.music.view.adapter.SelectSongPlayAdapter.SelectSongPlayListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class SelecSongPlay extends ActivityBase implements OnClickListener {
	public static final String KEY_LIST_SONG = "key list song";
	public static final String KEY_LIST_SONG_TITLE = "title list of this song";

	private ImageButton mexitBtn;
	private TextView mTitle;
	private TextView mNumberSongHasSelect;
	private ImageView mCheckAllSong;
	private ListView mListSong;
	private SelectSongPlayAdapter mAdapter;
	private LinearLayout mLayoutMenu;
	private ImageButton mPlayAllSelected;
	private ImageButton mInsertToNowPlaying;
	private ImageButton mAddtoPlaylist;
	private ImageButton mDownloadSongSelected;
	private Appinstant mApp;

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		mApp = (Appinstant) getApplication();
		setContentView(R.layout.select_song_play);
		mexitBtn = (ImageButton) findViewById(R.id.bt_exit);
		mTitle = (TextView) findViewById(R.id.tv_title_select_song_play);
		mTitle.setSelected(true);
		mListSong = (ListView) findViewById(R.id.lv_list_song_select);
		mLayoutMenu = (LinearLayout) findViewById(R.id.layout_menu_select_song_play);
		mPlayAllSelected = (ImageButton) findViewById(R.id.bt_play_all_in_select_song);
		mInsertToNowPlaying = (ImageButton) findViewById(R.id.bt_insert_to_now_play);
		mAddtoPlaylist = (ImageButton) findViewById(R.id.bt_add_to_play_list);
		mDownloadSongSelected = (ImageButton) findViewById(R.id.bt_downloadsong);
		mCheckAllSong = (ImageView) findViewById(R.id.cb_select_all_song);
		mNumberSongHasSelect = (TextView) findViewById(R.id.tv_number_song_had_select);
		mNumberSongHasSelect
				.setText(getString(R.string.you_not_select_any_song));

		mCheckAllSong.setOnClickListener(this);
		mexitBtn.setOnClickListener(this);
		mPlayAllSelected.setOnClickListener(this);
		mInsertToNowPlaying.setOnClickListener(this);
		mAddtoPlaylist.setOnClickListener(this);
		mDownloadSongSelected.setOnClickListener(this);

		String title = mApp.tranportString.remove(KEY_LIST_SONG_TITLE);/*
																	 * started.
																	 * getStringExtra
																	 * (
																	 * KEY_LIST_SONG_TITLE
																	 * );
																	 */
		mTitle.setText(title);
		Intent started = getIntent();
		String key = started.getStringExtra(KEY_LIST_SONG);
		if (key != null) {
			Appinstant app = (Appinstant) getApplication();
			List<Song> listSong = app.tranportListSong.remove(key);
			if (listSong != null) {
				for (Song song : listSong) {
					if (song.isLocalSong()) {
						mAddtoPlaylist.setVisibility(View.GONE);
						mDownloadSongSelected.setVisibility(View.GONE);
						break;
					}
				}
				for (Song song: listSong) {
					if (song.isAvailableLocally()) {
						mDownloadSongSelected.setVisibility(View.GONE);
					} else {
						mDownloadSongSelected.setVisibility(View.VISIBLE);
						break;
					}
				}
				
				mAdapter = new SelectSongPlayAdapter(this, listSong, mListener);
				mListSong.setAdapter(mAdapter);
				mListSong.setOnItemClickListener(songItemClickListener);
			}
		}
	}

	private SelectSongPlayListener mListener = new SelectSongPlayListener() {

		@Override
		public void changeNumberSongSelect() {
			// TODO Auto-generated method stub
			if (mAdapter == null) {
				return;
			}
			if (mAdapter.numberSelect <= 0) {
				mNumberSongHasSelect
						.setText(getString(R.string.you_not_select_any_song));
				if (mLayoutMenu.isShown()) {
					mLayoutMenu
							.startAnimation(AnimationChaCha.outToBottom(200));
					mLayoutMenu.setVisibility(View.GONE);
				}
			} else {
				mNumberSongHasSelect.setText("Đã chọn " + mAdapter.numberSelect
						+ "/" + mAdapter.getCount() + " bài hát");
				if (!mLayoutMenu.isShown()) {
					mLayoutMenu.startAnimation(AnimationChaCha
							.infFromBottom(200));
					mLayoutMenu.setVisibility(View.VISIBLE);
				}
			}
		}
	};

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	AdapterView.OnItemClickListener songItemClickListener = new AdapterView.OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
			mAdapter.changeStateForPosition(arg2);
		}
	};

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.bt_exit:
			finish();
			break;
		case R.id.cb_select_all_song:
			if (mCheckAllSong.isSelected()) {
				mCheckAllSong.setSelected(false);
				mAdapter.setSelectNone();
			} else {
				mCheckAllSong.setSelected(true);
				mAdapter.setSelectAll();
			}
			break;
		case R.id.bt_play_all_in_select_song:
			List<Song> listSelected = mAdapter.getListSelected();
			Util.openListSong(listSelected, 0, SelecSongPlay.this);
			finish();
			break;
		case R.id.bt_insert_to_now_play:
			listSelected = mAdapter.getListSelected();
			Util.insertListSongToNowPlaying(listSelected, SelecSongPlay.this);
			finish();
			// }
			break;
		case R.id.bt_add_to_play_list:
			if (!checkIsConnect()) {
				return;
			}
			listSelected = mAdapter.getListSelected();
			PlaylistUtil.addSongToPlaylist(listSelected, SelecSongPlay.this);
			break;
		case R.id.bt_downloadsong:
			if (!checkIsConnect()) {
				return;
			}
			listSelected = mAdapter.getListSelected();
			for (Song song : listSelected) {
				
			}
			DownloadUtil.downLoad(DownloadUtil.DOWNLOAD_LIST_SONG, listSelected, Se, warningMessage, showRegister)
			DownloadUtil.downloadListSong(listSelected, SelecSongPlay.this);
			finish();
			break;

		default:
			break;
		}
	}
}
